package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/http"
	"time"
)

func startHttpServer() *http.Server {
	srv := &http.Server{Addr: ":9090"}
	http.HandleFunc("/hello", helloServer)
	go func() {
		err := srv.ListenAndServe()
		if err != nil {
			log.Fatal("ListenAndServer error: ", err)
		}
	}()
	return srv
}

func HttpServer01() {
	srv := startHttpServer()
	fmt.Println("http server start")

	time.Sleep(10 * time.Second)

	fmt.Println("http server stop")
	if err := srv.Shutdown(context.TODO()); err != nil {
		panic(err)
	}
	fmt.Println("main execute done!")
}

func helloServer(w http.ResponseWriter, req *http.Request) {
	io.WriteString(w, "hello, world!\n")
}
